package com.bbg.amyam.web.mvc.security;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.security.core.GrantedAuthority;

import com.bbg.amyam.domain.model.Profile;
import com.bbg.amyam.domain.model.School;

public class UserRoleResolver {

	public static Collection<GrantedAuthority> listUserRoles(String username, String schoolShortName) {
		ArrayList<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
		AmyamGrantedAuthority granted=new AmyamGrantedAuthority(AmyamGrantedAuthority.ROLE_USER);
		authorities.add(granted);
		
		List<School> schools = School.findSchoolsByShortNameEquals(schoolShortName).getResultList();
		if(schools.size()>0){
			School school = schools.get(0);
			Profile profile = school.getAdminProfile();
			if(profile!=null){
				if(username.equals(profile.getEmail())){
					if("default".equals(schoolShortName)){
						granted=new AmyamGrantedAuthority(AmyamGrantedAuthority.ROLE_ROOT_ADMIN);
					}else{
						granted=new AmyamGrantedAuthority(AmyamGrantedAuthority.ROLE_SCHOOL_ADMIN);
					}
					authorities.add(granted);
				}
			}
		}
		return authorities;
	}

}
